package com.bangdao.juc;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author chen
 * @create 2021-01-03
 */
public class HashMapDemo {
    public static void main(String[] args) {
        //线程不安全
//        Map<String, String> map = new HashMap<>();
        //线程安全
//        Map<String,String> map = Collections.synchronizedMap(new HashMap<>());
        //线程安全
        Map<String,String> map = new ConcurrentHashMap<>();
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                map.put(UUID.randomUUID().toString().substring(0, 2), UUID.randomUUID().toString().substring(0, 8));
                System.out.println(map);
            }).start();
        }
    }
}
